package com.iotproject.eduservice.controller;


import com.iotproject.commonutils.R;
import com.iotproject.eduservice.entity.EduComment;
import com.iotproject.eduservice.service.EduCommentService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 评论 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2023-04-19
 */
@RestController
@RequestMapping("/eduservice/edu-comment")
public class EduCommentController {

    @Autowired
    EduCommentService eduCommentService;

    //插入评论
    @PostMapping("addComment")
    public R addComment(@RequestBody EduComment eduComment) {
        boolean save = eduCommentService.save(eduComment);
        if (save) {
            return R.ok();
        }
        return R.error();
    }

    //显示评论列表
    @GetMapping("commentList/{courseId}")
    public R commentList(@PathVariable("courseId") String courseId) {
        QueryWrapper<EduComment> eduCommentQueryWrapper = new QueryWrapper<>();
        eduCommentQueryWrapper.eq("course_id", courseId);
        eduCommentQueryWrapper.orderByDesc("gmt_create");
        List<EduComment> list = eduCommentService.list(eduCommentQueryWrapper);
        return R.ok().data("list", list).data("len", list.size());
    }


}

